{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b56d02eb",
   "metadata": {},
   "source": [
    "# 6-1,构建模型的3种方法\n",
    "\n",
    "可以使用以下3种方式构建模型：\n",
    "\n",
    "1，继承nn.Module基类构建自定义模型。\n",
    "\n",
    "2，使用nn.Sequential按层顺序构建模型。\n",
    "\n",
    "3，继承nn.Module基类构建模型并辅助应用模型容器进行封装(nn.Sequential,nn.ModuleList,nn.ModuleDict)。\n",
    "\n",
    "其中 第1种方式最为常见，第2种方式最简单，第3种方式最为灵活也较为复杂。\n",
    "\n",
    "推荐使用第1种方式构建模型。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "536f4c77",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch \n",
    "import torchkeras\n",
    "\n",
    "print(\"torch.__version__=\"+torch.__version__) \n",
    "print(\"torchkeras.__version__=\"+torchkeras.__version__) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8267bcfb",
   "metadata": {},
   "source": [
    "```\n",
    "torch.__version__=1.10.0\n",
    "torchkeras.__version__=3.2.3\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da6704ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a9c3d8f3",
   "metadata": {},
   "source": [
    "### 一，继承nn.Module基类构建自定义模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a03920c8",
   "metadata": {},
   "source": [
    "以下是继承nn.Module基类构建自定义模型的一个范例。模型中的用到的层一般在`__init__`函数中定义，然后在`forward`方法中定义模型的正向传播逻辑。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7852e18",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3)\n",
    "        self.pool1 = nn.MaxPool2d(kernel_size = 2,stride = 2)\n",
    "        self.conv2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5)\n",
    "        self.pool2 = nn.MaxPool2d(kernel_size = 2,stride = 2)\n",
    "        self.dropout = nn.Dropout2d(p = 0.1)\n",
    "        self.adaptive_pool = nn.AdaptiveMaxPool2d((1,1))\n",
    "        self.flatten = nn.Flatten()\n",
    "        self.linear1 = nn.Linear(64,32)\n",
    "        self.relu = nn.ReLU()\n",
    "        self.linear2 = nn.Linear(32,1)\n",
    "        \n",
    "    def forward(self,x):\n",
    "        x = self.conv1(x)\n",
    "        x = self.pool1(x)\n",
    "        x = self.conv2(x)\n",
    "        x = self.pool2(x)\n",
    "        x = self.dropout(x)\n",
    "        x = self.adaptive_pool(x)\n",
    "        x = self.flatten(x)\n",
    "        x = self.linear1(x)\n",
    "        x = self.relu(x)\n",
    "        y = self.linear2(x)\n",
    "        return y\n",
    "        \n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fdfcf14",
   "metadata": {},
   "source": [
    "```\n",
    "Net(\n",
    "  (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "  (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "  (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (dropout): Dropout2d(p=0.1, inplace=False)\n",
    "  (adaptive_pool): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "  (flatten): Flatten(start_dim=1, end_dim=-1)\n",
    "  (linear1): Linear(in_features=64, out_features=32, bias=True)\n",
    "  (relu): ReLU()\n",
    "  (linear2): Linear(in_features=32, out_features=1, bias=True)\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c76ceb33",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchkeras import summary \n",
    "summary(net,input_shape= (3,32,32));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0d91d44",
   "metadata": {},
   "source": [
    "```\n",
    "--------------------------------------------------------------------------\n",
    "Layer (type)                            Output Shape              Param #\n",
    "==========================================================================\n",
    "Conv2d-1                            [-1, 32, 30, 30]                  896\n",
    "MaxPool2d-2                         [-1, 32, 15, 15]                    0\n",
    "Conv2d-3                            [-1, 64, 11, 11]               51,264\n",
    "MaxPool2d-4                           [-1, 64, 5, 5]                    0\n",
    "Dropout2d-5                           [-1, 64, 5, 5]                    0\n",
    "AdaptiveMaxPool2d-6                   [-1, 64, 1, 1]                    0\n",
    "Flatten-7                                   [-1, 64]                    0\n",
    "Linear-8                                    [-1, 32]                2,080\n",
    "ReLU-9                                      [-1, 32]                    0\n",
    "Linear-10                                    [-1, 1]                   33\n",
    "==========================================================================\n",
    "Total params: 54,273\n",
    "Trainable params: 54,273\n",
    "Non-trainable params: 0\n",
    "--------------------------------------------------------------------------\n",
    "Input size (MB): 0.011719\n",
    "Forward/backward pass size (MB): 0.359627\n",
    "Params size (MB): 0.207035\n",
    "Estimated Total Size (MB): 0.578381\n",
    "--------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "191e6cd3",
   "metadata": {},
   "source": [
    "### 二，使用nn.Sequential按层顺序构建模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54335466",
   "metadata": {},
   "source": [
    "使用nn.Sequential按层顺序构建模型无需定义forward方法。仅仅适合于简单的模型。\n",
    "\n",
    "以下是使用nn.Sequential搭建模型的一些等价方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb064d65",
   "metadata": {},
   "source": [
    "1，利用add_module方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81633480",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "net = nn.Sequential()\n",
    "net.add_module(\"conv1\",nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3))\n",
    "net.add_module(\"pool1\",nn.MaxPool2d(kernel_size = 2,stride = 2))\n",
    "net.add_module(\"conv2\",nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5))\n",
    "net.add_module(\"pool2\",nn.MaxPool2d(kernel_size = 2,stride = 2))\n",
    "net.add_module(\"dropout\",nn.Dropout2d(p = 0.1))\n",
    "net.add_module(\"adaptive_pool\",nn.AdaptiveMaxPool2d((1,1)))\n",
    "net.add_module(\"flatten\",nn.Flatten())\n",
    "net.add_module(\"linear1\",nn.Linear(64,32))\n",
    "net.add_module(\"relu\",nn.ReLU())\n",
    "net.add_module(\"linear2\",nn.Linear(32,1))\n",
    "print(net)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f09fcc3",
   "metadata": {},
   "source": [
    "```\n",
    "Sequential(\n",
    "  (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "  (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "  (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (dropout): Dropout2d(p=0.1, inplace=False)\n",
    "  (adaptive_pool): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "  (flatten): Flatten(start_dim=1, end_dim=-1)\n",
    "  (linear1): Linear(in_features=64, out_features=32, bias=True)\n",
    "  (relu): ReLU()\n",
    "  (linear2): Linear(in_features=32, out_features=1, bias=True)\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "017389b7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "80a47503",
   "metadata": {},
   "source": [
    "2，利用变长参数\n",
    "\n",
    "这种方式构建时不能给每个层指定名称。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19f77a5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "net = nn.Sequential(\n",
    "    nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),\n",
    "    nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "    nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),\n",
    "    nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "    nn.Dropout2d(p = 0.1),\n",
    "    nn.AdaptiveMaxPool2d((1,1)),\n",
    "    nn.Flatten(),\n",
    "    nn.Linear(64,32),\n",
    "    nn.ReLU(),\n",
    "    nn.Linear(32,1)\n",
    ")\n",
    "\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "856b470c",
   "metadata": {},
   "source": [
    "```\n",
    "Sequential(\n",
    "  (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "  (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "  (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (4): Dropout2d(p=0.1, inplace=False)\n",
    "  (5): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "  (6): Flatten(start_dim=1, end_dim=-1)\n",
    "  (7): Linear(in_features=64, out_features=32, bias=True)\n",
    "  (8): ReLU()\n",
    "  (9): Linear(in_features=32, out_features=1, bias=True)\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54e68d20",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e8dec597",
   "metadata": {},
   "source": [
    "3，利用OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "989fde07",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "\n",
    "net = nn.Sequential(OrderedDict(\n",
    "          [(\"conv1\",nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3)),\n",
    "            (\"pool1\",nn.MaxPool2d(kernel_size = 2,stride = 2)),\n",
    "            (\"conv2\",nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5)),\n",
    "            (\"pool2\",nn.MaxPool2d(kernel_size = 2,stride = 2)),\n",
    "            (\"dropout\",nn.Dropout2d(p = 0.1)),\n",
    "            (\"adaptive_pool\",nn.AdaptiveMaxPool2d((1,1))),\n",
    "            (\"flatten\",nn.Flatten()),\n",
    "            (\"linear1\",nn.Linear(64,32)),\n",
    "            (\"relu\",nn.ReLU()),\n",
    "            (\"linear2\",nn.Linear(32,1))\n",
    "          ])\n",
    "        )\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd554f04",
   "metadata": {},
   "source": [
    "```\n",
    "Sequential(\n",
    "  (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "  (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "  (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "  (dropout): Dropout2d(p=0.1, inplace=False)\n",
    "  (adaptive_pool): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "  (flatten): Flatten(start_dim=1, end_dim=-1)\n",
    "  (linear1): Linear(in_features=64, out_features=32, bias=True)\n",
    "  (relu): ReLU()\n",
    "  (linear2): Linear(in_features=32, out_features=1, bias=True)\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0aac6e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchkeras import summary \n",
    "summary(net,input_shape= (3,32,32));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09554a10",
   "metadata": {},
   "source": [
    "```\n",
    "--------------------------------------------------------------------------\n",
    "Layer (type)                            Output Shape              Param #\n",
    "==========================================================================\n",
    "Conv2d-1                            [-1, 32, 30, 30]                  896\n",
    "MaxPool2d-2                         [-1, 32, 15, 15]                    0\n",
    "Conv2d-3                            [-1, 64, 11, 11]               51,264\n",
    "MaxPool2d-4                           [-1, 64, 5, 5]                    0\n",
    "Dropout2d-5                           [-1, 64, 5, 5]                    0\n",
    "AdaptiveMaxPool2d-6                   [-1, 64, 1, 1]                    0\n",
    "Flatten-7                                   [-1, 64]                    0\n",
    "Linear-8                                    [-1, 32]                2,080\n",
    "ReLU-9                                      [-1, 32]                    0\n",
    "Linear-10                                    [-1, 1]                   33\n",
    "==========================================================================\n",
    "Total params: 54,273\n",
    "Trainable params: 54,273\n",
    "Non-trainable params: 0\n",
    "--------------------------------------------------------------------------\n",
    "Input size (MB): 0.011719\n",
    "Forward/backward pass size (MB): 0.359627\n",
    "Params size (MB): 0.207035\n",
    "Estimated Total Size (MB): 0.578381\n",
    "--------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5be6178a",
   "metadata": {},
   "source": [
    "### 三，继承nn.Module基类构建模型并辅助应用模型容器进行封装"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff50ca5d",
   "metadata": {},
   "source": [
    "当模型的结构比较复杂时，我们可以应用模型容器(nn.Sequential,nn.ModuleList,nn.ModuleDict)对模型的部分结构进行封装。\n",
    "\n",
    "这样做会让模型整体更加有层次感，有时候也能减少代码量。\n",
    "\n",
    "注意，在下面的范例中我们每次仅仅使用一种模型容器，但实际上这些模型容器的使用是非常灵活的，可以在一个模型中任意组合任意嵌套使用。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d67d5aa9",
   "metadata": {},
   "source": [
    "1，nn.Sequential作为模型容器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39699388",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.conv = nn.Sequential(\n",
    "            nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),\n",
    "            nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "            nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),\n",
    "            nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "            nn.Dropout2d(p = 0.1),\n",
    "            nn.AdaptiveMaxPool2d((1,1))\n",
    "        )\n",
    "        self.dense = nn.Sequential(\n",
    "            nn.Flatten(),\n",
    "            nn.Linear(64,32),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(32,1)\n",
    "        )\n",
    "    def forward(self,x):\n",
    "        x = self.conv(x)\n",
    "        y = self.dense(x)\n",
    "        return y \n",
    "    \n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e9dd029",
   "metadata": {},
   "source": [
    "```\n",
    "Net(\n",
    "  (conv): Sequential(\n",
    "    (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "    (2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "    (4): Dropout2d(p=0.1, inplace=False)\n",
    "    (5): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "  )\n",
    "  (dense): Sequential(\n",
    "    (0): Flatten(start_dim=1, end_dim=-1)\n",
    "    (1): Linear(in_features=64, out_features=32, bias=True)\n",
    "    (2): ReLU()\n",
    "    (3): Linear(in_features=32, out_features=1, bias=True)\n",
    "  )\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4919cc62",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c32ddf69",
   "metadata": {},
   "source": [
    "2，nn.ModuleList作为模型容器\n",
    "\n",
    "注意下面中的ModuleList不能用Python中的列表代替。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bbddd64",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.layers = nn.ModuleList([\n",
    "            nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),\n",
    "            nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "            nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),\n",
    "            nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "            nn.Dropout2d(p = 0.1),\n",
    "            nn.AdaptiveMaxPool2d((1,1)),\n",
    "            nn.Flatten(),\n",
    "            nn.Linear(64,32),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(32,1)]\n",
    "        )\n",
    "    def forward(self,x):\n",
    "        for layer in self.layers:\n",
    "            x = layer(x)\n",
    "        return x\n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be03b9a8",
   "metadata": {},
   "source": [
    "```\n",
    "Net(\n",
    "  (layers): ModuleList(\n",
    "    (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "    (2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "    (4): Dropout2d(p=0.1, inplace=False)\n",
    "    (5): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "    (6): Flatten(start_dim=1, end_dim=-1)\n",
    "    (7): Linear(in_features=64, out_features=32, bias=True)\n",
    "    (8): ReLU()\n",
    "    (9): Linear(in_features=32, out_features=1, bias=True)\n",
    "  )\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c8053ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchkeras import summary \n",
    "summary(net,input_shape= (3,32,32));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a1374e3",
   "metadata": {},
   "source": [
    "```\n",
    "--------------------------------------------------------------------------\n",
    "Layer (type)                            Output Shape              Param #\n",
    "==========================================================================\n",
    "Conv2d-1                            [-1, 32, 30, 30]                  896\n",
    "MaxPool2d-2                         [-1, 32, 15, 15]                    0\n",
    "Conv2d-3                            [-1, 64, 11, 11]               51,264\n",
    "MaxPool2d-4                           [-1, 64, 5, 5]                    0\n",
    "Dropout2d-5                           [-1, 64, 5, 5]                    0\n",
    "AdaptiveMaxPool2d-6                   [-1, 64, 1, 1]                    0\n",
    "Flatten-7                                   [-1, 64]                    0\n",
    "Linear-8                                    [-1, 32]                2,080\n",
    "ReLU-9                                      [-1, 32]                    0\n",
    "Linear-10                                    [-1, 1]                   33\n",
    "==========================================================================\n",
    "Total params: 54,273\n",
    "Trainable params: 54,273\n",
    "Non-trainable params: 0\n",
    "--------------------------------------------------------------------------\n",
    "Input size (MB): 0.011719\n",
    "Forward/backward pass size (MB): 0.359627\n",
    "Params size (MB): 0.207035\n",
    "Estimated Total Size (MB): 0.578381\n",
    "--------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "010bbdca",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "dfa420e9",
   "metadata": {},
   "source": [
    "3，nn.ModuleDict作为模型容器\n",
    "\n",
    "注意下面中的ModuleDict不能用Python中的字典代替。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8264bafd",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.layers_dict = nn.ModuleDict({\"conv1\":nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),\n",
    "               \"pool\": nn.MaxPool2d(kernel_size = 2,stride = 2),\n",
    "               \"conv2\":nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),\n",
    "               \"dropout\": nn.Dropout2d(p = 0.1),\n",
    "               \"adaptive\":nn.AdaptiveMaxPool2d((1,1)),\n",
    "               \"flatten\": nn.Flatten(),\n",
    "               \"linear1\": nn.Linear(64,32),\n",
    "               \"relu\":nn.ReLU(),\n",
    "               \"linear2\": nn.Linear(32,1)\n",
    "              })\n",
    "    def forward(self,x):\n",
    "        layers = [\"conv1\",\"pool\",\"conv2\",\"pool\",\"dropout\",\"adaptive\",\n",
    "                  \"flatten\",\"linear1\",\"relu\",\"linear2\",\"sigmoid\"]\n",
    "        for layer in layers:\n",
    "            x = self.layers_dict[layer](x)\n",
    "        return x\n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09b4e071",
   "metadata": {},
   "source": [
    "```\n",
    "Net(\n",
    "  (layers_dict): ModuleDict(\n",
    "    (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))\n",
    "    (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
    "    (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))\n",
    "    (dropout): Dropout2d(p=0.1, inplace=False)\n",
    "    (adaptive): AdaptiveMaxPool2d(output_size=(1, 1))\n",
    "    (flatten): Flatten(start_dim=1, end_dim=-1)\n",
    "    (linear1): Linear(in_features=64, out_features=32, bias=True)\n",
    "    (relu): ReLU()\n",
    "    (linear2): Linear(in_features=32, out_features=1, bias=True)\n",
    "  )\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4893da89",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3f213794",
   "metadata": {},
   "source": [
    "**如果本书对你有所帮助，想鼓励一下作者，记得给本项目加一颗星星star⭐️，并分享给你的朋友们喔😊!** \n",
    "\n",
    "如果对本书内容理解上有需要进一步和作者交流的地方，欢迎在公众号\"算法美食屋\"下留言。作者时间和精力有限，会酌情予以回复。\n",
    "\n",
    "也可以在公众号后台回复关键字：**加群**，加入读者交流群和大家讨论。\n",
    "\n",
    "![算法美食屋logo.png](https://tva1.sinaimg.cn/large/e6c9d24egy1h41m2zugguj20k00b9q46.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9826641",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "formats": "ipynb,md",
   "main_language": "python"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
